<template>
  <div class="box">
    <h1>{{title}}</h1>
    <el-form
      :model="ruleForm2"
      status-icon
      :rules="rules2"
      ref="ruleForm2"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.number="ruleForm2.phone" placeholder="请输入您的手机号码"></el-input>
        <el-button @click="sendAuthCode('ruleForm2')" :disabled="flge">
          免费发送验证码
          <span>{{this.time}}</span>
        </el-button>
      </el-form-item>
      <el-form-item label="短信验证码" prop="pass">
        <el-input type="password" v-model="ruleForm2.pass" autocomplete="off" placeholder="请输入您验证码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from "axios";
// import $ from "jquery";
import Router from "vue-router";
export default {
  data () {
    var phone = (rule, value, callback) => {
      var value = value.toString().trim();
      if (!/^1[34578]\d{9}$/.test(value)) {
        return callback(new Error("电话号码有误"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("验证码不能为空"));
      } else if (value.length < 6) {
        callback(new Error("验证码为6位数"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    return {
      title: "手机号码注册",
      flge: false,
      authCode: "",
      time: "",
      phone: "",
      ruleForm2: {
        phone: ""
      },
      tipsmsg: "",
      rules2: {
        phone: [{ validator: phone, trigger: "blur" }]
      }
    };
  },
  methods: {
    success () {
      this.$message({
        message: this.tipsmsg,
        type: "success"
      });
    },
    fault () {
      this.$message.error(this.tipsmsg);
    },
    warning () {
      this.$notify({
        title: "警告",
        message: this.tipsmsg,
        type: "warning"
      });
    },
    sendAuthCode (formName) {
      var that = this;
      var phone = String(this.ruleForm2.phone);
      if (/^1[34578]\d{9}$/.test(phone)) {
        var setI = null;
        this.time = 60;
        // 禁用按钮
        this.flge = true;
        // 倒计时取消禁用按钮
        setI = setInterval(() => {
          that.time = --that.time;
          if (this.time == 0) {
            clearInterval(setI);
            this.time = "";
            this.flge = false;
          }
        }, 1000);
        // 请求验证码
        axios.post('api/mobile', {
          phone: phone
        }).then(res => {
          console.log(res)
          if (res.data.status == 0) {
            console.log(res.data);
            this.authCode = res.data.code;
            this.phone = res.data.phone;
            setTimeout(() => {
              this.authCode = "";
              this.tipsmsg = "刚才发的验证码过期啦，请重新获取验证码";
              this.warning();
            }, 60000);
          }
        }).catch(error => {
          console.log(error)
        })
      }
    },
    submitForm () {
      if (
        this.ruleForm2.pass == this.authCode &&
        this.ruleForm2.pass != "" &&
        this.phone == this.ruleForm2.phone
      ) {
        this.tipsmsg = "注册成功";
        this.success();
      } else {
        this.tipsmsg = "验证码错误或电话号码有误";
        this.fault();
        return false;
      }
    }
  }
};
</script>
<style lang="stylus" scoped>
.box {
  min-width: 320px;
  max-width: 440px;
  height: 290px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  background: #f0f0f0;
  border-radius: 10px;
  padding-right: 40px;
}

.cookies_box {
  display: block;
}

h1 {
  font-size: 20px;
  color: #333;
  text-align: center;
  padding: 20px;
  margin-left: 40px;
}

from {
  width: 500px;
}

.register {
  display: block;
  background: #66b1ff;
  border-color: #66b1ff;
  color: #fff;
}
</style>
